<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="课程编号" prop="courseNo">
        <el-input
          v-model="queryParams.courseNo"
          placeholder="请输入课程编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
            <el-form-item label="课程试卷ID" prop="coursePaperId">
        <el-input
          v-model="queryParams.coursePaperId"
          placeholder="请输入课程试卷ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程试卷名称" prop="paperName">
        <el-input
          v-model="queryParams.paperName"
          placeholder="请输入课程试卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="试卷分类" prop="paperType">
        <el-select v-model="queryParams.paperType" placeholder="请选择试卷分类" clearable>
          <el-option
            v-for="dict in dict.type.sys_paper_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="paperStatus">
        <el-select v-model="queryParams.paperStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="checkStatus">
        <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.paper_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入创建者昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:paper:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:paper:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:paper:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:paper:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程试卷ID" align="center" prop="coursePaperId" />
      <el-table-column label="试卷名称" align="center" prop="paperName">
        <template slot-scope="scope">
            <router-link :to="'/courseTopic-sub/index/' + scope.row.courseNo+'/' + scope.row.coursePaperId" class="link-type">
              <span>{{ scope.row.paperName }}</span>
            </router-link>
        </template>
      </el-table-column>
      <el-table-column label="课程编号" align="center" prop="courseNo"/>
      <el-table-column label="课程名称" align="center" prop="courseName">
        <template slot-scope="scope">
          <span>{{scope.row.courseName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试时长" align="center" prop="elapsedTime">
        <template slot-scope="scope">
          <span>{{scope.row.elapsedTime }}分钟</span>
        </template>
      </el-table-column>
      <el-table-column label="试卷分类" align="center" prop="paperType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_paper_type" :value="scope.row.paperType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="paperStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.paperStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="考试人数" align="center" prop="studentNum" />
      <el-table-column label="审核状态" align="center" prop="checkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.paper_check_status" :value="scope.row.checkStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="试题总数" align="center" prop="topicNum" />
      <el-table-column label="备注" align="center" prop="paperRemark" :show-overflow-tooltip="true" />
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="nickName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['course:paper:query']"
          >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:paper:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['course:paper:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程试卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="课程名称" prop="courseNo">
          <el-select v-model="form.courseNo"  filterable placeholder="请选择课程名称">
            <el-option
            v-for="item in courseOption"
            :key="item.courseNo"
            :value="item.courseNo"
            :label="item.courseTypeName + ' - ' + item.courseName"
            :disabled="item.status == 1"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="试卷" prop="paperId">
          <el-select v-model="form.paperId"  filterable placeholder="请选择试卷">
            <el-option
            v-for="item in paperOption"
            :key="item.paperId"
            :value="item.paperId"
            :label="item.paperName"
            :disabled="item.paperStatus == 1"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="考试时长(分钟)" prop="elapsedTime">
          <el-input-number v-model="form.elapsedTime" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="试卷分类" prop="paperType">
          <el-select v-model="form.paperType" placeholder="请选择试卷分类">
            <el-option
              v-for="dict in dict.type.sys_paper_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="paperStatus">
          <el-radio-group v-model="form.paperStatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核状态" prop="checkStatus">
          <el-select v-model="form.checkStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.paper_check_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="paperRemark">
          <el-input v-model="form.paperRemark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详细start -->
    <el-dialog :title="title" :visible.sync="paperView"  width="900px" append-to-body>
      <el-form ref="viewForm" :model="viewForm" label-width="120px" size="mini">
        <el-form-item label="课程试卷信息："></el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程试卷ID：">
              <span>{{ viewForm.coursePaperId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="试卷名称：">
              <span>{{ viewForm.paperName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程编号：">
              <span>{{ viewForm.courseNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程名称：">
              <span>{{ viewForm.courseName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="试卷分类：">
              <dict-tag :options="dict.type.sys_paper_type" :value="viewForm.paperType" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试时长：">
              <span>{{ viewForm.elapsedTime }}分钟</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="试题总数：">
              <span>{{ viewForm.topicNum }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考试人数：">
              {{viewForm.studentNum}}
            </el-form-item>
          </el-col>
        </el-row>
                <el-row>
          <el-col :span="12">
            <el-form-item label="审核状态：">
              <dict-tag :options="dict.type.paper_check_status" :value="viewForm.checkStatus" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态：">
              <dict-tag :options="dict.type.sys_normal_disable" :value="viewForm.paperStatus" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间：">
              <span>{{ parseTime(viewForm.beginTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成时间：">
             <span>{{ parseTime(viewForm.finishTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="创建者：">
              <span>{{ viewForm.nickName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建日期：">
              <span>{{
                parseTime(viewForm.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="题目信息：" style="margin-top: 1.5rem"></el-form-item>
        <el-table v-loading="topicLoading" :data="topicList">
          <el-table-column label="题目ID" align="center" prop="courseTopicId" />
          <el-table-column label="试卷名称" align="center" prop="paperName" :show-overflow-tooltip="true" />
          <el-table-column label="题目名称" align="center" prop="topicName" :show-overflow-tooltip="true" />
          <el-table-column label="题目说明" align="center" prop="topicIntro"  :show-overflow-tooltip="true" />
          <el-table-column label="题目类型" align="center" prop="topicType" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_topic_type" :value="scope.row.topicType"/>
            </template>
          </el-table-column>
          <el-table-column label="题目难度" align="center" prop="topicHard" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_topic_hard" :value="scope.row.topicHard"/>
            </template>
          </el-table-column>
          <el-table-column label="题目备注" align="center" prop="topicRemark" />
        </el-table>
        <pagination
          v-show="topicTotal > 0"
          :total="topicTotal"
          :page.sync="topicParams.pageNum"
          :limit.sync="topicParams.pageSize"
          @pagination="getTopicList"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="paperView = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 详细end -->
  </div>
</template>

<script>
import { listPaper, getPaper, delPaper, addPaper, updatePaper } from "@/api/course/coursePaper";
import { listTopic } from "@/api/course/courseTopic";
import { wholeList } from "@/api/course/course";
import { wholePaper } from "@/api/course/paper";


export default {
  name: "coursePaper",
  dicts: ['sys_normal_disable','sys_paper_type','paper_check_status','sys_topic_type','sys_topic_hard'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程试卷表格数据
      paperList: [],
      // 课程数据
      courseOption: [],
      // 课程题目表格数据
      topicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      paperView: false,
      //题目列表
      topicLoading: true,
      // 题目总条数
      topicTotal: 0,
      // 题目记录查询参数
      topicParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: undefined,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseNo: null,
        coursePaperId: null,
        courseName: null,
        nickName: null,
        paperName: null,
        paperType: null,
        paperStatus: null,
        checkStatus: null,
        elapsedTime: null,
        userId: null,
      },
      // 表单参数
      form: {},
      //详情表单参数
      viewForm: {},
      // 表单校验
      rules: {
        courseNo: [
          { required: true, message: "请选择课程", trigger: "blur" }
        ],
        paperId: [
          { required: true, message: "请选择试卷", trigger: "blur" }
        ],
        paperType: [
          { required: true, message: "请选择试卷分类", trigger: "blur" }
        ],
        paperStatus: [
          { required: true, message: "请选择试卷状态", trigger: "blur" }
        ],
        checkStatus: [
          { required: true, message: "请选择审核状态", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCourseList();
  },
  methods: {
    /** 查询课程试卷列表 */
    getList() {
      this.loading = true;
      if(this.$route.params.courseNo !=null){
        this.queryParams.courseNo = this.$route.params.courseNo;
        this.$route.params.courseNo = null;
      }
      if(this.$route.params.coursePaperId !=null){
        this.queryParams.coursePaperId = this.$route.params.coursePaperId;
        this.$route.params.coursePaperId = null;
      }
      listPaper(this.queryParams).then(response => {
        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询课程列表 */
    getCourseList() {
      wholeList(this.queryParams).then(response => {
        this.courseOption = response.data;
      });
    },
    /** 查询试卷列表 */
    getPaperList() {
      wholePaper(this.queryParams).then(response => {
        this.paperOption = response.data;
        this.queryParams.checkStatus = null;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        coursePaperId: null,
        courseNo: null,
        paperId: null,
        paperName: null,
        paperType: 1,
        paperStatus: 0,
        studentNum: null,
        checkStatus: 1,
        topicNum: null,
        score: null,
        elapsedTime: null,
        paperRemark: null,
        userId: null,
        delFlag: null,
        beginTime: null,
        finishTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.coursePaperId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 查询课程题目列表 */
    getTopicList(row) {
      this.topicParams.courseNo = row.courseNo;
      listTopic(this.topicParams).then(response => {
        this.topicList = response.rows;
        this.topicTotal = response.total;
        this.topicLoading = false;
      });
    },

    /** 详细按钮操作 */
    handleView(row) {
       const coursePaperId = row.coursePaperId;
       getPaper(coursePaperId).then(response => {
        this.viewForm = response.data;
        this.paperView = true;
        this.title = "课程试卷详情";
      });
      /** 查询课程题目列表 */
      this.getTopicList(row);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      //只搜索已经审核通过的
      this.queryParams.checkStatus = 2;
      this.getPaperList();
      this.open = true;
      this.title = "添加课程试卷";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //只搜索已经审核通过的
      this.queryParams.checkStatus = 2;
      this.getPaperList();
      const coursePaperId = row.coursePaperId || this.ids
      getPaper(coursePaperId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程试卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.coursePaperId != null) {
            updatePaper(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaper(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const coursePaperIds = row.coursePaperId || this.ids;
      this.$modal.confirm('是否确认删除课程试卷编号为"' + coursePaperIds + '"的数据项？').then(function() {
        return delPaper(coursePaperIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/paper/export', {
        ...this.queryParams
      }, `coursePaper_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
